<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\ArticleKind;
use app\admin\model\ProductKind;
use app\admin\model\Navigation;
class System extends Common
{
	//基本设置
	public function config(){;
		$inc_type = input('get.inc_type')?intput('get.inc_type'):'shop_info';
		$res = db('config')->where("inc_type='$inc_type'")->select();
		$config = array();
		if($res){
            foreach($res as $k=>$val){
                $config[$val['name']] = $val['value'];
            }
        }

        $this->assign('inc_type',$inc_type);
        $this->assign('config_data',$config);
		return view();
	}

	//保存 基本设置 修改
	public function saveconfig() {
        $postData = input('post.');
        $postData['copyright'] = stripcslashes($postData['copyright']);
        $inc_type = $postData['inc_type'];
        unset($postData['inc_type']);
        //查询数据库所有
        $result = db('config')->where("inc_type='$inc_type'")->select();
        if($result){
        	foreach($result as $val){
        		$temp[$val['name']] = $val['value'];
        	}
        }
        //和post得到数据对比，$postData
        foreach($postData as $k=>$v){
        	$newArr = array('name'=>$k,'value'=>trim($v),'inc_type'=>$inc_type);
        	if(!isset($temp[$k])){
        		db('config')->insertGetId($newArr);//添加
        	}else{
        		if($v!=$temp[$k]){
        			db('config')->where("name='$k'")->update($newArr);//更新
        		}
        	}
        }
        /*********************记录日志***********************/
        addLog(session('user_name').'修改全局设置成功！');
        $json = array('status'=>1,'message'=>'设置成功！');
        echo json_encode($json);exit;
	}

	/**
     * 导航列表
     * FunctionName: navigationList
     * @author : roc
     */

    public function navigationList(){
        $NavigationCat = new Navigation(); 
        $navigationList = $NavigationCat->navigation_list(0,0,false);
        $this->assign('navigationList',$navigationList);    

        return view('navigationList');
    }

    /**
     * 导航添加修改
     * FunctionName: navigationList
     * @author : roc
     */
    public  function addEditNav(){
    	if(request()->isPost()){
    		$data = input('post.');
    		$id = $data['id'];
    		if($id){
                if ($data['id'] == $data['parent_id']) 
                {
                    //$this->error("所选分类的上级分类不能是当前分类",U('Admin/Article/category',array('id'=>$data['id'])));
                    $json['status'] = 0;
                    $json['message'] = "所选分类的上级分类不能是当前分类！";
                    echo json_encode($json);
                    return;
                }
                $NavigationCat = new Navigation();
                $children = array_keys($NavigationCat->navigation_list($data['id'], 0, false)); // 获得当前分类的所有下级分类
                if (in_array($data['parent_id'], $children))
                {
                    //$this->error("所选分类的上级分类不能是当前分类的子分类",U('Admin/Article/category',array('id'=>$data['id'])));
                    $json['status'] = 0;
                    $json['message'] = "所选分类的上级分类不能是当前分类的子分类！";
                    echo json_encode($json);
                    return;
                }
    			unset($data['id']);
    			db('navigation')->where(array('id'=>$id))->update($data);
                /*********************记录日志***********************/
                addLog(session('user_name').'修改导航成功-'.$data['name'].' [ ID:'.$id.' ]');
    		}else{
    			$re = db('navigation')->insertGetId($data);
                /*********************记录日志***********************/
                addLog(session('user_name').'添加导航成功-'.$data['name'].' [ ID:'.$re.' ]');
    		}
    		$this->success("操作成功!!!",url('Admin/System/navigationList'));
            exit;
    	}else{
    		if(input('route.id')){
    			$id = input('route.id');
    			$navigation = db('navigation')->find($id);
    		}
    		$this->assign('navigation',$navigation);
    		//文章栏目
    		$ArticleCat = new ArticleKind(); 
	        $cats = $ArticleCat->article_kind_list(0,$navigation['cate_id'],true);
	        $this->assign('article_kind',$cats);
            //产品栏目
            $ProductCat = new ProductKind();
            $cats = $ProductCat->product_kind_list(0,$navigation['category_id'],true);
            $this->assign('product_kind',$cats);
            //
            $NavigationCat = new Navigation(); 
            $pid = empty($navigation['parent_id'])?'':$navigation['parent_id'];
            $cats = $NavigationCat->navigation_list(0,$pid,true);
            $this->assign('cat_select',$cats);

    		return view('navigation');
    	}
    }

    /**
     * 删除前台 自定义 导航
     */
    public function delNav($id){
        $data = db('navigation')->where("id = {$id}")->find();
        $r = db('navigation')->where("id = {$id}")->delete();
        if($r){
            $return_arr = array('status' => 1, 'msg'    => '删除成功', 'data'  => url('Admin/System/navigationList'));
            /*********************记录日志***********************/
            addLog(session('user_name').'删除导航成功-'.$data['name'].' [ ID:'.$id.' ]');
            echo json_encode($return_arr);exit;
        }else{
            $return_arr = array('status' => -1, 'msg'   => '操作失败', 'data'  => url('Admin/System/navigationList'));
            echo json_encode($return_arr);exit;
        }
    }

    /**
     * 模板管理
     */
    function templateList(){
        $list = db('template')->order('sort asc')->select();
        $this->assign('list',$list);
        return view('templateList');
    }

    function template() {
        $id = input('route.id');
        $cat_info = array();
        if($id){
            $act = 'edit';
            $cat_info = db('template')->where('id='.$id)->find();
            $this->assign('cat_info',$cat_info);
        }else{
            $act = 'add';
        }
        $this->assign('act',$act);
        return view();
    }

    function templateHandle(){
        $data = input('post.');
        $data['sort'] = input('post.sort') ? input('post.sort') : 99;
        $data['is_hide'] = input('post.is_hide') ? input('post.is_hide') : 0;
        $act = $data['act'];
        unset($data['act']);
        if($act == 'add'){ 
            $d = db('template')->insertGetId($data);
            if($d!==false){
                $json['status'] = 1;
                $json['message'] = "添加模板成功！";
                echo json_encode($json);
                /*********************记录日志***********************/
                addLog(session('user_name').'添加模板成功-'.$data['template'].' [ ID:'.$d.' ]');
                return;
            }
        }else if($act == 'edit'){
            $id = $data['id'];
            unset($data['id']);
            $d = db('template')->where(array('id'=>$id))->update($data);
            if($d!==false){
                $json['status'] = 1;
                $json['message'] = "修改模板成功！";
                echo json_encode($json);
                /*********************记录日志***********************/
                addLog(session('user_name').'修改模板成功-'.$data['template'].' [ ID:'.$id.' ]');
                return;
            }
        }else if($act == 'del'){
            $data = db('template')->where(array('id'=>$data['id']))->find();
            $d = db('template')->where(array('id'=>$data['id']))->delete();
            if($d!==false){
                $json['status'] = 1;
                $json['message'] = "删除模板成功！";
                echo json_encode($json);
                /*********************记录日志***********************/
                addLog(session('user_name').'删除模板成功-'.$data['template'].' [ ID:'.$data['id'].' ]');
                return;
            }
        }
    }

    function log_list(){
        return view();
    }
    function ajax_log_list(){
        $get_data = input("get.");
        $data = input("post.");

        $list = array();
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $size = empty($_REQUEST['size']) ? 25 : $_REQUEST['size'];
        
        $where[] = ['log_time','>=',strtotime($data['start_time'])];
        $where[] = ['log_time','<=',strtotime($data['end_time'])];
        $count = db('admin_log')->where($where)->count();// 查询满足要求的总记录数

        $list = db('admin_log')->where($where)->order('id desc')->paginate($size,$count)->each(function($item,$key){
            return $item;
        });
        $page = $list->render();

        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('count',$count);

        return view();
    }
    function log_detail(){
        $id = input('route.id');
        if($id>0){
            $one = db('admin_log')->where('id='.$id)->find();
            $content = json_decode($one['content'],true);
            $this->assign('one',$one);
            $this->assign('content',$content);
        }
        return view();
    }
    function log_del(){
        $id = input('post.id');
        if($id == session('log_id')){
            $json = array('status'=>0,'message'=>'当前登录日志状态使用中，不能删除');
            echo json_encode($json);exit;
        }
        $res = db('admin_log')->where('id='.$id)->delete();
        if($res){
            $json = array('status'=>1,'message'=>'删除日志成功');
        }else{
            $json = array('status'=>0,'message'=>'删除日志失败');
        }
        echo json_encode($json);exit;
    }

    function client_log(){
        return view();
    }

    function ajax_client_log(){
        $get_data = input("get.");
        $data = input("post.");

        $list = array();
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $size = empty($_REQUEST['size']) ? 10 : $_REQUEST['size'];
        
        if(!empty($data['start_time'])){
            $where[] = ['addtime','>=',date('Y-m-d H:i:s',strtotime($data['start_time']))];
        }
        if(!empty($data['end_time'])){
            $where[] = ['addtime','<=',date('Y-m-d H:i:s',strtotime($data['end_time']))];
        }
        
        if(!empty($data['keyword'])){
            $where[] = ['ip','like','%'.$data['keyword'].'%'];
        }
        $count = db('client_log')->where($where)->count();// 查询满足要求的总记录数

        $list = db('client_log')->where($where)->order('id desc')->paginate($size,$count)->each(function($item,$key){
            return $item;
        });
        $page = $list->render();

        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('count',$count);

        return view();
    }

    function client_log_del(){
        $id = input('post.id');
        $res = db('client_log')->where('id='.$id)->delete();
        if($res){
            $json = array('status'=>1,'message'=>'删除日志成功');
        }else{
            $json = array('status'=>0,'message'=>'删除日志失败');
        }
        echo json_encode($json);exit;
    }

}